﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Data;
using SmartEP.Model;
using SmartEP.DTO;
using SmartEP.DataService;

namespace SmartEP.DataService
{
    public class UserService
    {
        private UserModel _UserModel;

        public UserService()
        {
            _UserModel = new UserModel();
        }

        public User AuthenticateUser(User user)
        {
            DataSet ds = _UserModel.AuthenticateUser(user);
            User userReturn = new User();

            if (ds != null && ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    DataView dv = _UserModel.AuthenticateUser(user).Tables[0].DefaultView;
                    
                    userReturn.Username = Convert.ToString(BaseModel.GetFieldValue(dv[0], UserModel.USER_NAME_FIELD));
                    userReturn.Password = Convert.ToString(BaseModel.GetFieldValue(dv[0], UserModel.PASSWORD_FIELD));
                    userReturn.Birthday = Convert.ToDateTime(BaseModel.GetFieldValue(dv[0], UserModel.BIRTHDAY_FIELD));
                    userReturn.FirstName = Convert.ToString(BaseModel.GetFieldValue(dv[0], UserModel.FIRST_NAME_FIELD));
                    userReturn.LastName = Convert.ToString(BaseModel.GetFieldValue(dv[0], UserModel.LAST_NAME_FIELD));
                    userReturn.MiddleName = Convert.ToString(BaseModel.GetFieldValue(dv[0], UserModel.MIDDLE_FIELD ));
                    userReturn.Phone = Convert.ToString(BaseModel.GetFieldValue(dv[0], UserModel.PHONE_FIELD));
                    userReturn.CorrectPassword = Convert.ToBoolean(BaseModel.GetFieldValue(dv[0], "CorrectPassword"));
                   
                }
            }
            return userReturn;
        }

        public void WriteLoginXML(string FileName, User User)
        {
            _UserModel.WriteLoginXML(FileName, User);
        }

        public ArrayList ReadLoginXML(string FileName)
        {
           return  _UserModel.ReadLoginXML(FileName);
        }
    }
}
